package com.example.cardgame

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Bundle
import android.util.Base64
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    private val REQUEST_CODE_INFO = 1
    private val REQUEST_CODE_CARD = 2
    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // 假设这些信息是从某个数据源获取的
        val playerName = "孙朝栋"
        val playerGender = "男"
        val playerPhone = "2022110541"
        val playerHighScore = 0
        // 设置玩家信息
        val tvName = findViewById<TextView>(R.id.player_name)
        val tvPhone = findViewById<TextView>(R.id.player_phone)
        val tvgender = findViewById<TextView>(R.id.player_gender)
        val tvHighScore = findViewById<TextView>(R.id.player_high_score)

        tvName.text = "Name: $playerName"
        tvPhone.text = "Phone: $playerPhone"
        tvgender.text = "Gender: $playerGender"
        tvHighScore.text = "High Score: $playerHighScore"
        // 个人信息修改按钮点击事件
        val btnEditInfo: Button = findViewById(R.id.info)
        btnEditInfo.setOnClickListener {
            val intent = Intent(this, Infoactivity::class.java)
            startActivityForResult(intent, REQUEST_CODE_INFO)
        }
        // 翻牌游戏按钮点击事件
        val btnCardGame: Button = findViewById(R.id.game)
        btnCardGame.setOnClickListener {
            // 跳转到翻牌游戏Activity
            val intent = Intent(this, Cardactivity::class.java)
            startActivityForResult(intent, REQUEST_CODE_CARD)
        }
    }
    // 处理返回的数据
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_CODE_INFO && resultCode == RESULT_OK) {
            data?.let {
                val playerName = it.getStringExtra("name")
                val playerPhone = it.getStringExtra("phone")
                val playerGender = it.getStringExtra("gender")
                val encodedImage = it.getStringExtra("image")
                findViewById<TextView>(R.id.player_name).text = "Name: $playerName"
                findViewById<TextView>(R.id.player_phone).text = "Phone: $playerPhone"
                findViewById<TextView>(R.id.player_gender).text = "Gender: $playerGender"
                val byteArray = Base64.decode(encodedImage, Base64.DEFAULT)
                val imageBitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.size)
                findViewById<ImageView>(R.id.imageView).setImageBitmap(imageBitmap)
            }
        } else if (requestCode == REQUEST_CODE_CARD && resultCode == RESULT_OK) {
            data?.let {
                val highScore = it.getIntExtra("highScore", 0)
                findViewById<TextView>(R.id.player_high_score).text = "High Score: $highScore"
            }
        }
    }
}
